﻿using Nop.Services.Tasks;
using Nop.Web.Services.Factories;
using System;
using System.Collections.Generic;
using System.Text;
using Nop.Services.Logging;
using Nop.Web.Services.Utils;
using Nop.Services.Catalog;
using Nop.Web.Services.Mob;

namespace Nop.Web.Services.Task
{
    public class HPayWalletQueueTask : IScheduleTask
    {
        const string TAG = "钱包返点任务:";

        private readonly ILogger _logger;
        private readonly HUCFactory _hUCFactory;
        private readonly HPaySdkHelper _hPaySdkHelper;
        private readonly IProductService _productService;
        private readonly MobPushHelper _pushHelper;

        public HPayWalletQueueTask(ILogger logger, HUCFactory hUCFactory, HPaySdkHelper hPaySdkHelper, IProductService productService, MobPushHelper pushHelper)
        {
            _logger = logger;
            _hUCFactory = hUCFactory;
            _hPaySdkHelper = hPaySdkHelper;
            _productService = productService;
            _pushHelper = pushHelper;
        }

        public void Execute()
        {
            try
            {
                var list = _hUCFactory.UnPassWalletQueueList();
                foreach (var queue in list)
                {
                    try
                    {
                        var locked = _hUCFactory.LockWalletQueue(queue.Id);
                        if (locked)
                        {
                            try
                            {
                                var sdk = _hPaySdkHelper.Get();
                                var walletResult = sdk.GetWallet(queue.HUC_MemberId, queue.WalletType).Result;
                                bool hasWallet = walletResult.Success;
                                var template = _productService.GetWalletTemplate(queue.WalletType);
                                if (!hasWallet && walletResult.Code.Equals("8007"))
                                {
                                    var createResult = sdk.CreateWalletAsync(queue.HUC_MemberId, queue.WalletType, template.Name, template.IconUrl).Result;
                                    hasWallet = createResult.Success;
                                }
                                if (hasWallet)
                                {
                                    var grpcResult = sdk.WalletCheckin(queue.HUC_MemberId, queue.WalletType, Convert.ToInt32(queue.Amount * 100), queue.Describe, $"Nop_Queue_{queue.Id}").Result;
                                    if (grpcResult.Success && grpcResult.TradeNo > 0)
                                    {
                                        string mobAlia = string.Format("huc{0}", queue.HUC_MemberId);
                                        _pushHelper.Send(mobAlia, "订单分润", $"{template.Name}余额增加{queue.Amount.ToString("0.00")}", new
                                        {
                                            memberId = queue.HUC_MemberId,
                                            type = "walletChanged",
                                            ext = new
                                            {
                                                tradeNo = grpcResult.TradeNo,
                                                walletType = queue.WalletType,
                                                amount = queue.Amount

                                            }
                                        }); 
                                        _hUCFactory.UnLockWalletQueueOfPass(queue.Id);
                                    }
                                    else
                                    {
                                        _hUCFactory.UnLockWalletQueueOfError(queue.Id);
                                    }
                                }
                                else
                                {
                                    _logger.Warning($"{TAG}队列[{queue.Id}创建钱包失败");
                                    _hUCFactory.UnLockWalletQueueOfError(queue.Id);
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.Warning($"{TAG}队列[{queue.Id}执行失败", ex);
                                _hUCFactory.UnLockWalletQueueOfError(queue.Id);
                            }
                        }
                        else
                        {
                            _logger.Warning($"{TAG}队列[{queue.Id}]锁定失败");
                        }
                    }
                    catch (Exception ex)
                    {

                        _logger.Error($"{TAG}单个队列错误", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"{TAG}任务错误", ex);
            }
        }
    }
}
